package com.gzy.rpsm_property.controller;

import com.gzy.rpsm_property.constant.FreightBasisData;
import com.gzy.rpsm_property.mapper.FreightBasisMapper;
import com.gzy.rpsm_property.pojo.FreightBasis;
import com.gzy.rpsm_property.pojo.ResponsePojo;
import com.gzy.rpsm_property.service.FreightBasisService;
import com.gzy.rpsm_property.utils.StringUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController
@RequestMapping("/freightBasis")
@Api(tags = "费率相关 API")
public class FreightBasisController {
    @Autowired
    FreightBasisData freightBasisData;
    @Autowired
    FreightBasisService freightBasisService;


    /**
     * 查询费用说明的API接口
     *
     * @return 返回包含费用说明数据的响应对象
     */
    @ApiOperation(value = "查询费用说明")
    @ApiParam
    @PostMapping("/queryAll")
    public ResponsePojo<FreightBasisData> queryAll() {
        // 构建并返回成功响应，包含费用说明数据
        return ResponsePojo.success(freightBasisData);
    }


    /**
     * 修改费用说明的API接口，可以修改单价、说明或名称
     *
     /* @param basisPrice 单价，两位小数
     /* @param intro 费用介绍
     /* @param basis_name 费用名称
     /* @param freight_basis_id 费用id
     /* @param fh FreightBasis对象，用于封装费用信息
     /* @param res HttpServletResponse对象，用于发送HTTP响应
     /* @return 返回包含操作结果的响应对象
     **/
    @ApiOperation(value = "修改费用说明，可以修改单价、说明或名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "basisPrice", value = "单价，两位小数"),
            @ApiImplicitParam(name = "intro", value = "费用介绍"),
            @ApiImplicitParam(name = "basis_name", value = "费用名称"),
            @ApiImplicitParam(name = "freight_basis_id", value = "费用id", dataType = "int")
    })
    @PutMapping("/updateById")
    public ResponsePojo<Integer> update(String basisPrice, @ApiIgnore FreightBasis fh, HttpServletResponse res) {
        try {// 将字符串形式的单价转换为整数
            fh.setPrice(StringUtil.stringPriceToInt(basisPrice));
        } catch (Exception e) {
            try {// 发送403错误响应，并包含错误信息
                res.sendError(403, e.getMessage());
            } catch (IOException ioException) {
                // 打印堆栈跟踪信息
                ioException.printStackTrace();
            }
            // 返回null表示处理失败
            return null;
        }
        // 调用服务层方法更新费用信息
        int result = freightBasisService.updateById(fh);
        // 根据更新结果返回不同的响应
        switch (result) {
            case 1:
                freightBasisData.flushData();
                return ResponsePojo.success(result);
            case 0:
                return ResponsePojo.fail(0, "该 id 没有找到对应信息");
            default:
                return ResponsePojo.fail(2, "参数有误，修改失败");
        }
    }

}
